/* $Id: VBoxMPIf.h $ */
/** @file
 * VBox WDDM Miniport driver.
 *
 * Contains base definitions of constants & structures used to control & perform
 * rendering, such as DMA commands types, allocation types, escape codes, etc.
 * used by both miniport & display drivers.
 *
 * The latter uses these and only these defs to communicate with the former
 * by posting appropriate requests via D3D RT Krnl Svc accessing callbacks.
 */

/*
 * Copyright (C) 2011-2020 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

#ifndef _WDDM_INCLUDE_COMMON_VBOXMPIF_H_
#define _WDDM_INCLUDE_COMMON_VBOXMPIF_H_

#include <ntddk.h>
#include <dispmprt.h>
#include "CommonDef.h"
#include "VBoxGaHWInfo.h"
#include "VBoxVideo.h"

 /* VMSVGA headers. */
#pragma pack(1) /* VMSVGA structures are '__packed'. */
#include "gallium/svga_reg.h"
#include "gallium/svga3d_reg.h"
#pragma pack()

/* One would increase this whenever definitions in this file are changed */
#define VBOXVIDEOIF_VERSION 22

#define VBOXWDDM_ROUNDBOUND(_v, _b) (((_v) + ((_b) - 1)) & ~((_b) - 1))

/** Convert a given FourCC code to a D3DDDIFORMAT enum. */
#define VBOXWDDM_D3DDDIFORMAT_FROM_FOURCC(_a, _b, _c, _d) \
    ((D3DDDIFORMAT)MAKEFOURCC(_a, _b, _c, _d))

#define VBOXWDDM_NODE_ID_SYSTEM             0
#define VBOXWDDM_NODE_ID_3D                 (VBOXWDDM_NODE_ID_SYSTEM)
#define VBOXWDDM_NODE_ID_3D_KMT             (VBOXWDDM_NODE_ID_3D)
#define VBOXWDDM_NODE_ID_2D_VIDEO           (VBOXWDDM_NODE_ID_3D_KMT + 1)
#define VBOXWDDM_NUM_NODES                  (VBOXWDDM_NODE_ID_2D_VIDEO + 1)

#define GA_MAX_SURFACE_FACES 6
#define GA_MAX_MIP_LEVELS 24

typedef struct SVGAGBSURFCREATE
{
    /* Surface data. */
    struct
    {
        SVGA3dSurfaceAllFlags flags;
        SVGA3dSurfaceFormat format;
        UINT32 usage;
        SVGA3dSize size;
        UINT32 numFaces;
        UINT32 numMipLevels;
        UINT32 sampleCount;
        SVGA3dMSPattern multisamplePattern;
        SVGA3dMSQualityLevel qualityLevel;
    } s;
    UINT32 gmrid; /* In/Out: Backing GMR. */
    UINT32 cbGB; /* Out: Size of backing memory. */
    UINT64 u64UserAddress; /* Out: R3 mapping of the backing memory. */
    UINT32 u32Sid; /* Out: Surface id. */
} SVGAGBSURFCREATE, * PSVGAGBSURFCREATE;

typedef struct GASURFCREATE
{
    UINT32 flags;  /* SVGA3dSurfaceFlags */
    UINT32 format; /* SVGA3dSurfaceFormat */
    UINT32 usage;  /* SVGA_SURFACE_USAGE_* */
    UINT32 mip_levels[GA_MAX_SURFACE_FACES];
} GASURFCREATE;

typedef struct GASURFSIZE
{
    UINT32 cWidth;
    UINT32 cHeight;
    UINT32 cDepth;
    UINT32 u32Reserved;
} GASURFSIZE;

/* Query a user mode fence object state. */
#define GA_FENCE_STATUS_NULL      0 /* Fence not found */
#define GA_FENCE_STATUS_IDLE      1
#define GA_FENCE_STATUS_SUBMITTED 2
#define GA_FENCE_STATUS_SIGNALED  3

/** @todo VBOXVIDEO_HWTYPE probably needs to be in VBoxVideo.h */
typedef enum VBOXVIDEO_HWTYPE
{
    VBOXVIDEO_HWTYPE_VBOX   = 0,
    VBOXVIDEO_HWTYPE_VMSVGA = 1,
    VBOXVIDEO_HWTYPE_32BIT  = 0x7fffffff
} VBOXVIDEO_HWTYPE;

typedef struct VBOXWDDM_SURFACE_DESC
{
    UINT width;
    UINT height;
    D3DDDIFORMAT format;
    UINT bpp;
    UINT pitch;
    UINT depth;
    UINT slicePitch;
    UINT d3dWidth;
    UINT cbSize;
    D3DDDI_VIDEO_PRESENT_SOURCE_ID VidPnSourceId;
    D3DDDI_RATIONAL RefreshRate;
} VBOXWDDM_SURFACE_DESC, *PVBOXWDDM_SURFACE_DESC;

#if defined(VBOX_WITH_VMSVGA3D_DX)

#include <dxgiddi.h>

/* D3DDDI_ALLOCATIONINFO::pPrivateDriverData */
typedef enum VBOXDXALLOCATIONTYPE
{
    VBOXDXALLOCATIONTYPE_UNKNOWN = 0,
    VBOXDXALLOCATIONTYPE_SURFACE = 1,
    VBOXDXALLOCATIONTYPE_SHADERS = 2,
    VBOXDXALLOCATIONTYPE_CO      = 3,
    VBOXDXALLOCATIONTYPE_MAX,
    VBOXDXALLOCATIONTYPE_32BIT = 0xFFFFFFFF
} VBOXDXALLOCATIONTYPE;

/* Information for DxgkDdiCreateAllocation and for SVGA3dCmdDefine[GB]Surface. */
typedef struct VBOXDXALLOCATIONDESC
{
    VBOXDXALLOCATIONTYPE   enmAllocationType;
    UINT32               cbAllocation;
    struct
    {
        SVGA3dSurfaceAllFlags surfaceFlags;
        SVGA3dSurfaceFormat format;
        UINT32 numMipLevels;
        UINT32 multisampleCount;
        SVGA3dMSPattern multisamplePattern;
        SVGA3dMSQualityLevel qualityLevel;
        SVGA3dTextureFilter autogenFilter;
        SVGA3dSize size;
        UINT32 arraySize;
        UINT32 bufferByteStride;
    } surfaceInfo;
    bool                        fPrimary : 1;
    DXGI_DDI_PRIMARY_DESC       PrimaryDesc;
    D3DDDIFORMAT                enmDDIFormat;
    struct
    {
        /* These will be needed by OpenResource */
        UINT                    BindFlags;
        UINT                    MapFlags;
        UINT                    MiscFlags;
        DXGI_FORMAT             Format;
        UINT                    DecoderBufferType;          /* D3D11_1DDI_VIDEO_DECODER_BUFFER_TYPE */
    } resourceInfo;
} VBOXDXALLOCATIONDESC, *PVBOXDXALLOCATIONDESC;
#endif /* defined(VBOX_WITH_VMSVGA3D_DX) */

/* create allocation func */
typedef enum
{
    VBOXWDDM_ALLOC_TYPE_UNEFINED = 0,
    VBOXWDDM_ALLOC_TYPE_STD_SHAREDPRIMARYSURFACE,
    VBOXWDDM_ALLOC_TYPE_STD_SHADOWSURFACE,
    VBOXWDDM_ALLOC_TYPE_STD_STAGINGSURFACE,
    /* this one is win 7-specific and hence unused for now */
    VBOXWDDM_ALLOC_TYPE_STD_GDISURFACE,
    /* custom allocation types requested from user-mode d3d module will go here */
    VBOXWDDM_ALLOC_TYPE_UMD_RC_GENERIC,
    VBOXWDDM_ALLOC_TYPE_UMD_HGSMI_BUFFER,
    VBOXWDDM_ALLOC_TYPE_D3D /* Direct3D UMD driver allocation. Actual type is a VBOXDXALLOCATIONTYPE value. */
} VBOXWDDM_ALLOC_TYPE;

typedef struct VBOXWDDM_RC_DESC
{
    D3DDDI_RESOURCEFLAGS fFlags;
    D3DDDIFORMAT enmFormat;
    D3DDDI_POOL enmPool;
    D3DDDIMULTISAMPLE_TYPE enmMultisampleType;
    UINT MultisampleQuality;
    UINT MipLevels;
    UINT Fvf;
    D3DDDI_VIDEO_PRESENT_SOURCE_ID VidPnSourceId;
    D3DDDI_RATIONAL RefreshRate;
    D3DDDI_ROTATION enmRotation;
} VBOXWDDM_RC_DESC, *PVBOXWDDM_RC_DESC;

typedef struct VBOXWDDM_ALLOCINFO
{
    VBOXWDDM_ALLOC_TYPE enmType;
    union
    {
        struct
        {
            D3DDDI_RESOURCEFLAGS fFlags;
            /* id used to identify the allocation on the host */
            UINT32 hostID;
            UINT64 hSharedHandle;
            VBOXWDDM_SURFACE_DESC SurfDesc;
        };

        struct
        {
            UINT32 cbBuffer;
            //VBOXUHGSMI_BUFFER_TYPE_FLAGS fUhgsmiType;  //DO NOT delete!! used in future
        };
    };
} VBOXWDDM_ALLOCINFO, *PVBOXWDDM_ALLOCINFO;

typedef struct VBOXWDDMDISP_RESOURCE_FLAGS
{
    union
    {
        struct
        {
            UINT Opened     : 1; /* this resource is OpenResource'd rather than CreateResource'd */
            UINT Generic    : 1; /* identifies this is a resource created with CreateResource, the VBOXWDDMDISP_RESOURCE::fRcFlags is valid */
            UINT KmResource : 1; /* this resource has underlying km resource */
            UINT Reserved   : 29; /* reserved */
        };
        UINT Value;
    };
} VBOXWDDMDISP_RESOURCE_FLAGS, *PVBOXWDDMDISP_RESOURCE_FLAGS;

typedef struct VBOXWDDM_RCINFO
{
    VBOXWDDMDISP_RESOURCE_FLAGS fFlags;
    VBOXWDDM_RC_DESC RcDesc;
    UINT32 cAllocInfos;
} VBOXWDDM_RCINFO, *PVBOXWDDM_RCINFO;

typedef enum
{
    VBOXWDDM_CONTEXT_TYPE_UNDEFINED = 0,
    /* system-created context (for GDI rendering) */
    VBOXWDDM_CONTEXT_TYPE_SYSTEM,
    /* context created by the D3D User-mode driver when crogl IS available */
    obsolete_VBOXWDDM_CONTEXT_TYPE_CUSTOM_3D,
    /* context created by the D3D User-mode driver when crogl is NOT available or for ddraw overlay acceleration */
    obsolete_VBOXWDDM_CONTEXT_TYPE_CUSTOM_2D,
    /* contexts created by the cromium HGSMI transport for HGSMI commands submission */
    obsolete_VBOXWDDM_CONTEXT_TYPE_CUSTOM_UHGSMI_3D,
    obsolete_VBOXWDDM_CONTEXT_TYPE_CUSTOM_UHGSMI_GL,
    /* context created by the kernel->user communication mechanism for visible rects reporting, etc.  */
    VBOXWDDM_CONTEXT_TYPE_CUSTOM_SESSION,
    /* context created by VBoxTray to handle resize operations */
    VBOXWDDM_CONTEXT_TYPE_CUSTOM_DISPIF_RESIZE,
    /* context created by VBoxTray to handle seamless operations */
    VBOXWDDM_CONTEXT_TYPE_CUSTOM_DISPIF_SEAMLESS,
    /* Gallium driver context. */
    VBOXWDDM_CONTEXT_TYPE_GA_3D,
    /* Direct3D UMD context for VMSVGA device. */
    VBOXWDDM_CONTEXT_TYPE_VMSVGA_D3D,
} VBOXWDDM_CONTEXT_TYPE;

typedef struct VBOXWDDM_CREATECONTEXT_INFO
{
    /* interface version, i.e. 9 for d3d9, 8 for d3d8, etc. */
    UINT32 u32IfVersion;
    /* What kind of context to create. */
    VBOXWDDM_CONTEXT_TYPE enmType;
    union
    {
        struct
        {
            UINT32 crVersionMajor;
            UINT32 crVersionMinor;
            /* we use uint64_t instead of HANDLE to ensure structure def is the same for both 32-bit and 64-bit
             * since x64 kernel driver can be called by 32-bit UMD */
            UINT64 hUmEvent;
            /* info to be passed to UMD notification to identify the context */
            UINT64 u64UmInfo;
        } vbox;
        struct
        {
            /* VBOXWDDM_F_GA_CONTEXT_* */
            UINT32 u32Flags;
        } vmsvga;
    } u;
    /* Process name of current app. It will be passed to backend. */
    CHAR appName[MAX_PATH];
    UINT32 appNameLen;
} VBOXWDDM_CREATECONTEXT_INFO, *PVBOXWDDM_CREATECONTEXT_INFO;

/* tooling */
static inline UINT vboxWddmCalcBitsPerPixel(D3DDDIFORMAT enmFormat)
{
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable:4063) /* VBOXWDDM_D3DDDIFORMAT_FROM_FOURCC('Y', 'V', '1', '2'): isn't part of the enum */
#endif
    switch (enmFormat)
    {
        case D3DDDIFMT_R8G8B8:
            return 24;
        case D3DDDIFMT_A8R8G8B8:
        case D3DDDIFMT_X8R8G8B8:
            return 32;
        case D3DDDIFMT_R5G6B5:
        case D3DDDIFMT_X1R5G5B5:
        case D3DDDIFMT_A1R5G5B5:
        case D3DDDIFMT_A4R4G4B4:
            return 16;
        case D3DDDIFMT_R3G3B2:
        case D3DDDIFMT_A8:
            return 8;
        case D3DDDIFMT_A8R3G3B2:
        case D3DDDIFMT_X4R4G4B4:
            return 16;
        case D3DDDIFMT_A2B10G10R10:
        case D3DDDIFMT_A8B8G8R8:
        case D3DDDIFMT_X8B8G8R8:
        case D3DDDIFMT_G16R16:
        case D3DDDIFMT_A2R10G10B10:
            return 32;
        case D3DDDIFMT_A16B16G16R16:
        case D3DDDIFMT_A16B16G16R16F:
            return 64;
        case D3DDDIFMT_A32B32G32R32F:
            return 128;
        case D3DDDIFMT_A8P8:
            return 16;
        case D3DDDIFMT_P8:
        case D3DDDIFMT_L8:
            return 8;
        case D3DDDIFMT_L16:
        case D3DDDIFMT_A8L8:
            return 16;
        case D3DDDIFMT_A4L4:
            return 8;
        case D3DDDIFMT_V8U8:
        case D3DDDIFMT_L6V5U5:
            return 16;
        case D3DDDIFMT_X8L8V8U8:
        case D3DDDIFMT_Q8W8V8U8:
        case D3DDDIFMT_V16U16:
        case D3DDDIFMT_W11V11U10:
        case D3DDDIFMT_A2W10V10U10:
            return 32;
        case D3DDDIFMT_D16_LOCKABLE:
        case D3DDDIFMT_D16:
        case D3DDDIFMT_D15S1:
            return 16;
        case D3DDDIFMT_D32:
        case D3DDDIFMT_D24S8:
        case D3DDDIFMT_D24X8:
        case D3DDDIFMT_D24X4S4:
        case D3DDDIFMT_D24FS8:
        case D3DDDIFMT_D32_LOCKABLE:
        case D3DDDIFMT_D32F_LOCKABLE:
            return 32;
        case D3DDDIFMT_S8_LOCKABLE:
            return 8;
        case D3DDDIFMT_DXT1:
            return 4;
        case D3DDDIFMT_DXT2:
        case D3DDDIFMT_DXT3:
        case D3DDDIFMT_DXT4:
        case D3DDDIFMT_DXT5:
        case D3DDDIFMT_VERTEXDATA:
        case D3DDDIFMT_INDEX16: /* <- yes, dx runtime treats it as such */
            return 8;
        case D3DDDIFMT_INDEX32:
            return 8;
        case D3DDDIFMT_R32F:
            return 32;
        case D3DDDIFMT_G32R32F:
            return 64;
        case D3DDDIFMT_R16F:
            return 16;
        case D3DDDIFMT_G16R16F:
            return 32;
        case D3DDDIFMT_YUY2: /* 4 bytes per 2 pixels. */
        case VBOXWDDM_D3DDDIFORMAT_FROM_FOURCC('Y', 'V', '1', '2'):
            return 16;
        default:
            return 0;
    }
#ifdef _MSC_VER
# pragma warning(pop)
#endif
}

static inline UINT vboxWddmCalcOffXru(UINT w, D3DDDIFORMAT enmFormat)
{
    switch (enmFormat) {
        /* pitch for the DXT* (aka compressed) formats is the size in bytes of blocks that fill in an image width
         * i.e. each block decompressed into 4 x 4 pixels, so we have ((Width + 3) / 4) blocks for Width.
         * then each block has 64 bits (8 bytes) for DXT1 and 64+64 bits (16 bytes) for DXT2-DXT5, so.. : */
        case D3DDDIFMT_DXT1: {
            UINT Pitch = (w + 3) / 4; /* <- pitch size in blocks */
            Pitch *= 8;               /* <- pitch size in bytes */
            return Pitch;
        }
        case D3DDDIFMT_DXT2:
        case D3DDDIFMT_DXT3:
        case D3DDDIFMT_DXT4:
        case D3DDDIFMT_DXT5: {
            UINT Pitch = (w + 3) / 4; /* <- pitch size in blocks */
            Pitch *= 16;              /* <- pitch size in bytes */
            return Pitch;
        }
        default: {
            /* the default is just to calculate the pitch from bpp */
            UINT bpp = vboxWddmCalcBitsPerPixel(enmFormat);
            UINT Pitch = bpp * w;
            /* pitch is now in bits, translate in bytes */
            return VBOXWDDM_ROUNDBOUND(Pitch, 8) >> 3;
        }
    }
}

static inline UINT vboxWddmCalcPitch(UINT w, D3DDDIFORMAT enmFormat)
{
    return vboxWddmCalcOffXru(w, enmFormat);
}

static inline UINT vboxWddmCalcHightPacking(D3DDDIFORMAT enmFormat)
{
    switch (enmFormat)
    {
        /* for the DXT* (aka compressed) formats each block is decompressed into 4 x 4 pixels,
         * so packing is 4
         */
        case D3DDDIFMT_DXT1:
        case D3DDDIFMT_DXT2:
        case D3DDDIFMT_DXT3:
        case D3DDDIFMT_DXT4:
        case D3DDDIFMT_DXT5:
            return 4;
        default:
            return 1;
    }
}

static inline UINT vboxWddmCalcOffYru(UINT height, D3DDDIFORMAT enmFormat)
{
    UINT packing = vboxWddmCalcHightPacking(enmFormat);
    /* round it up */
    return (height + packing - 1) / packing;
}

static inline UINT vboxWddmCalcOffYrd(UINT height, D3DDDIFORMAT enmFormat)
{
    UINT packing = vboxWddmCalcHightPacking(enmFormat);
    /* round it up */
    return height / packing;
}

static inline UINT vboxWddmCalcNumRows(UINT top, UINT bottom, D3DDDIFORMAT enmFormat)
{
    top = top ? vboxWddmCalcOffYrd(top, enmFormat) : 0; /* <- just to optimize it a bit */
    bottom = vboxWddmCalcOffYru(bottom, enmFormat);
    return bottom - top;
}

static inline UINT vboxWddmCalcSize(UINT pitch, UINT height, D3DDDIFORMAT enmFormat)
{
    UINT cRows = vboxWddmCalcNumRows(0, height, enmFormat);
    return pitch * cRows;
}

#define VBOXWDDM_F_GA_CONTEXT_EXTENDED 0x00000001
#define VBOXWDDM_F_GA_CONTEXT_VGPU10   0x00000002

#define VBOXESC_GAGETCID            0xA0000002
#define VBOXESC_GAREGION            0xA0000003
#define VBOXESC_GAPRESENT           0xA0000004
#define VBOXESC_GASURFACEDEFINE     0xA0000005
#define VBOXESC_GASURFACEDESTROY    0xA0000006
#define VBOXESC_GASHAREDSID         0xA0000008
#define VBOXESC_GAFENCECREATE       0xA0000020
#define VBOXESC_GAFENCEQUERY        0xA0000021
#define VBOXESC_GAFENCEWAIT         0xA0000022
#define VBOXESC_GAFENCEUNREF        0xA0000023
#define VBOXESC_SVGAGBSURFACEDEFINE 0xA0010001
#define VBOXESC_SVGAGETSID          0xA0010002
#define VBOXESC_GALOGTOKMD          0xA0000030
#define VBOXESC_GETDISPSIZE         0xA0000040
#define VBOXESC_CREATEDECODER       0xA0000050
#define VBOXESC_DESTROYDECODER      (VBOXESC_CREATEDECODER + 1)
#define VBOXESC_GETDECODERID        (VBOXESC_CREATEDECODER + 2)
#define VBOXESC_SETVIDEODECODERT    (VBOXESC_CREATEDECODER + 3)
#define VBOXESC_DECODEBEGINFRAME    (VBOXESC_CREATEDECODER + 4)
#define VBOXESC_DECODEEXECUTE       (VBOXESC_CREATEDECODER + 5)
#define VBOXESC_DECODEENDFRAME      (VBOXESC_CREATEDECODER + 6)
#define VBOXESC_DECODESURFACEINFO   (VBOXESC_CREATEDECODER + 7)
#define VBOXESC_STRETCHRECT         (VBOXESC_CREATEDECODER + 8)

typedef struct VBOXDISPIFESCAPE
{
    INT32 escapeCode;
    UINT32 u32CmdSpecific;
} VBOXDISPIFESCAPE, *PVBOXDISPIFESCAPE;

typedef struct _VBOXDISPIFESCAPE_GETDISPSIZE {
    VBOXDISPIFESCAPE EscapeHdr;
    UINT32 Width;
    UINT32 Height;
} VBOXDISPIFESCAPE_GETDISPSIZE, *PVBOXDISPIFESCAPE_GETDISPSIZE;

typedef struct VBOXDISPIFESCAPE_LOGTOKMD
{
    VBOXDISPIFESCAPE EscapeHdr;
    char str[512];
} VBOXDISPIFESCAPE_LOGTOKMD;

/* Get Gallium context id (cid) of the WDDM context. */
typedef struct VBOXDISPIFESCAPE_GAGETCID
{
    VBOXDISPIFESCAPE EscapeHdr;
    UINT32 Cid;
} VBOXDISPIFESCAPE_GAGETCID;

/* Create or delete a Guest Memory Region (GMR). */
#define GA_REGION_CMD_CREATE  0
#define GA_REGION_CMD_DESTROY 1
typedef struct VBOXDISPIFESCAPE_GAREGION
{
    VBOXDISPIFESCAPE EscapeHdr;
    UINT32 u32Command;
    UINT32 u32GmrId;
    UINT32 u32NumPages;
    UINT32 u32Reserved;
    UINT64 u64UserAddress;
} VBOXDISPIFESCAPE_GAREGION;

/* Debug helper. Present the specified surface by copying to the guest screen VRAM. */
typedef struct VBOXDISPIFESCAPE_GAPRESENT
{
    VBOXDISPIFESCAPE EscapeHdr;
    UINT32 u32Sid;
    UINT32 u32Width;
    UINT32 u32Height;
} VBOXDISPIFESCAPE_GAPRESENT;

/* Create a host surface. */
typedef struct VBOXDISPIFESCAPE_GASURFACEDEFINE
{
    VBOXDISPIFESCAPE EscapeHdr;
    UINT32 u32Sid; /* Returned surface id. */
    UINT32 cbReq;  /* Size of data after cSizes field. */
    UINT32 cSizes; /* Number of GASURFSIZE structures. */
} VBOXDISPIFESCAPE_GASURFACEDEFINE;

/* Create a GB host surface. */
typedef struct VBOXDISPIFESCAPE_SVGAGBSURFACEDEFINE
{
    VBOXDISPIFESCAPE EscapeHdr;
    SVGAGBSURFCREATE CreateParms;
} VBOXDISPIFESCAPE_SVGAGBSURFACEDEFINE;

/* Get SVGA surface id (sid) of the allocation. */
typedef struct VBOXDISPIFESCAPE_SVGAGETSID
{
    VBOXDISPIFESCAPE EscapeHdr;
    UINT64 hAllocation;
    UINT32 u32Sid;
} VBOXDISPIFESCAPE_SVGAGETSID;

/* Delete a host surface. */
typedef struct VBOXDISPIFESCAPE_GASURFACEDESTROY
{
    VBOXDISPIFESCAPE EscapeHdr;
    UINT32 u32Sid;
} VBOXDISPIFESCAPE_GASURFACEDESTROY;

/* Inform the miniport that 'u32Sid' actually maps to 'u32SharedSid'.
 * If 'u32SharedSid' is ~0, then remove the mapping.
 */
typedef struct VBOXDISPIFESCAPE_GASHAREDSID
{
    VBOXDISPIFESCAPE EscapeHdr;
    UINT32 u32Sid;
    UINT32 u32SharedSid;
} VBOXDISPIFESCAPE_GASHAREDSID;

/* Create a user mode fence object. */
typedef struct VBOXDISPIFESCAPE_GAFENCECREATE
{
    VBOXDISPIFESCAPE EscapeHdr;

    /* IN: The miniport's handle of the fence.
     * Assigned by the miniport. Not DXGK fence id!
     */
    UINT32 u32FenceHandle;
} VBOXDISPIFESCAPE_GAFENCECREATE;

typedef struct VBOXDISPIFESCAPE_GAFENCEQUERY
{
    VBOXDISPIFESCAPE EscapeHdr;

    /* IN: The miniport's handle of the fence.
     * Assigned by the miniport. Not DXGK fence id!
     */
    UINT32 u32FenceHandle;

    /* OUT: The miniport's sequence number associated with the command buffer.
     */
    UINT32 u32SubmittedSeqNo;

    /* OUT: The miniport's sequence number associated with the last command buffer completed on host.
     */
    UINT32 u32ProcessedSeqNo;

    /* OUT: GA_FENCE_STATUS_*. */
    UINT32 u32FenceStatus;
} VBOXDISPIFESCAPE_GAFENCEQUERY;

/* Wait on a user mode fence object. */
typedef struct VBOXDISPIFESCAPE_GAFENCEWAIT
{
    VBOXDISPIFESCAPE EscapeHdr;

    /* IN: The miniport's handle of the fence.
     * Assigned by the miniport. Not DXGK fence id!
     */
    UINT32 u32FenceHandle;

    /* IN: Timeout in microseconds.
     */
    UINT64 u64TimeoutNS;
} VBOXDISPIFESCAPE_GAFENCEWAIT;

/* Delete a user mode fence object. */
typedef struct VBOXDISPIFESCAPE_GAFENCEUNREF
{
    VBOXDISPIFESCAPE EscapeHdr;

    /* IN: The miniport's handle of the fence.
     * Assigned by the miniport. Not DXGK fence id!
     */
    UINT32 u32FenceHandle;
} VBOXDISPIFESCAPE_GAFENCEUNREF;

#define VBOXWDDM_TRAILARRAY_MAXELEMENTSU32(_t, _af) ((UINT32)(((~(0UL)) - (UINT32)RT_OFFSETOF(_t, _af[0])) / RT_SIZEOFMEMB(_t, _af[0])))

#define VBOXVHWA_MAX_FORMATS 8
typedef struct VBOXVHWA_INFO
{
    UINT32 fFlags;
    UINT32 cOverlaysSupported;
    UINT32 cFormats;
    D3DDDIFORMAT aFormats[VBOXVHWA_MAX_FORMATS];
} VBOXVHWA_INFO;

#define VBOXWDDM_QAI_CAP_3D     0x00000001 /* 3D is enabled in the VM settings. */
#define VBOXWDDM_QAI_CAP_DXVA   0x00000002 /* DXVA is not disabled in the guest registry. */
#define VBOXWDDM_QAI_CAP_DXVAHD 0x00000004 /* DXVA-HD is not disabled in the guest registry. */
#define VBOXWDDM_QAI_CAP_WIN7   0x00000008 /* User mode driver can report D3D_UMD_INTERFACE_VERSION_WIN7. */

/* D3DDDICB_QUERYADAPTERINFO::pPrivateDriverData */
typedef struct VBOXWDDM_QAI
{
    UINT32            u32Version;      /* VBOXVIDEOIF_VERSION */
    UINT32            u32Reserved;     /* Must be 0. */
    VBOXVIDEO_HWTYPE  enmHwType;       /* Hardware type. Determines what kind of data is returned. */
    UINT32            u32AdapterCaps;  /* VBOXWDDM_QAI_CAP_* */
    UINT32            cInfos;          /* Number of initialized elements in aInfos (equal to number of guest
                                          * displays). 0 if VBOX_WITH_VIDEOHWACCEL is not defined. */
    VBOXVHWA_INFO     aInfos[VBOX_VIDEO_MAX_SCREENS]; /* cInfos elements are initialized. */
    union
    {
        struct
        {
            /* VBOXVIDEO_HWTYPE_VBOX */
            UINT32    u32VBox3DCaps;   /* CR_VBOX_CAP_* */
        } vbox;
        struct
        {
            /* VBOXVIDEO_HWTYPE_VMSVGA */
            VBOXGAHWINFO HWInfo;
        } vmsvga;
    } u;
} VBOXWDDM_QAI;

/* Dxva2 Video Device */
typedef struct VBoxVideoDecoderDesc {
    GUID         DecodeGuid;
    UINT         SampleWidth;
    UINT         SampleHeight;
    D3DDDIFORMAT Format;
    UINT         UABProtectionLevel;
}VBoxVideoDecoderDesc;

typedef struct VBoxVideoDecoderConfigPic
{
    GUID         guidConfigBitstreamEncryption;
    GUID         guidConfigMBcontrolEncryption;
    GUID         guidConfigResidDiffEncryption;
    UINT         ConfigBitstreamRaw;
    UINT         ConfigMBcontrolRasterOrder;
    UINT         ConfigResidDiffHost;
    UINT         ConfigSpatialResid8;
    UINT         ConfigResid8Subtraction;
    UINT         ConfigSpatialHost8or9Clipping;
    UINT         ConfigSpatialResidInterleaved;
    UINT         ConfigIntraResidUnsigned;
    UINT         ConfigResidDiffAccelerator;
    UINT         ConfigHostInverseScan;
    UINT         ConfigSpecificIDCT;
    UINT         Config4GroupedCoefs;
    USHORT       ConfigMinRenderTargetBuffCount;
    USHORT       ConfigDecoderSpecific;
}VBoxVideoDecoderConfigPic;

typedef struct VBOXDISPIFESCAPE_CREATEDECODER {
    VBOXDISPIFESCAPE          EscapeHdr;
    UINT32                    u32DecoderId;
    VBoxVideoDecoderDesc      desc;
    VBoxVideoDecoderConfigPic pic;
}VBOXDISPIFESCAPE_CREATEDECODER;

typedef struct VBOXDISPIFESCAPE_DECODERID {
    VBOXDISPIFESCAPE          EscapeHdr;
    UINT32                    u32DecoderId;
}VBOXDISPIFESCAPE_DECODERID;

typedef struct VBOXDISPIFESCAPE_SETVIDEODECODERT {
    VBOXDISPIFESCAPE EscapeHdr;
    UINT32 u32DecoderId;
    UINT32 u32Sid;
    UINT32 u32SubIndex;
}VBOXDISPIFESCAPE_SETVIDEODECODERT;

typedef struct VBOXDISPIFESCAPE_DECODEENDFRAME
{
    VBOXDISPIFESCAPE EscapeHdr;
    UINT32 u32DecoderId;
}VBOXDISPIFESCAPE_DECODEENDFRAME;

typedef struct {
    UINT32 uVideoDecoderId;
    UINT32 bufferCount;
}VBOXDISPIFSCAPE_DECODEEXECUTHEADER;

typedef struct VBOXDISPIFESCAPE_DECODEEXECUTE {
    VBOXDISPIFESCAPE EscapeHdr;
    VBOXDISPIFSCAPE_DECODEEXECUTHEADER ExeHeader;
} VBOXDISPIFESCAPE_DECODEEXECUTE;

typedef struct VBOXDISPIFSCAPE_VIDEODECODERBUFFERDESC {
    UINT32 sidBuffer;
    UINT32 bufferType;
    UINT32 dataOffset;
    UINT32 dataSize;
    UINT32 firstMBaddress;
    UINT32 numMBsInBuffer;
} VBOXDISPIFSCAPE_VIDEODECODERBUFFERDESC;

typedef struct VBOXDISPIFESCAPE_DECODESURFACEINFO {
    VBOXDISPIFESCAPE EscapeHdr;
    UINT32 u32SurfaceCount;
}VBOXDISPIFESCAPE_DECODESURFACEINFO;

typedef struct VBOXDISPIFESCAPE_STRETCHRECT {
    VBOXDISPIFESCAPE EscapeHdr;
    UINT32 uSrcSid;
    UINT32 uDstSid;
    RECT   SrcRect;
    RECT   DstRect;
} VBOXDISPIFESCAPE_STRETCHRECT;
#endif
